package com.example.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.example.dao.ProductRepository;
import com.example.entity.Product;

public class MyInterceptor implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
//		ProductRepository productDao = getDao(ProductRepository.class, request);
//		List<Product> findAll = productDao.findAll();
//		System.out.println(findAll.size());
		System.out.println(request.getRequestURI());
//		if("/error".equals(request.getRequestURI())){
//			response.sendRedirect("error.jsp");
//			return false;
//		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}
	
	private <T>T getDao(Class<T>clazz,HttpServletRequest request){
		BeanFactory factory=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		return factory.getBean(clazz);
	}
	

}
